<?php
// +----------------------------------------------------------------------
// | 管理员控制器
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\service\SysUserService;
use app\admin\service\SysRoleService;
use app\common\enum\ErrorEnum;
use think\facade\View;
use app\common\enum\BaseStatusEnum;
use app\common\enum\SexEnum;
use app\admin\service\SysDeptService;
use app\admin\service\SysPostService;

class SysUser extends AdminController
{
    public function index(){
        $res = SysUserService::page($this->request->get());

        $deptList = SysDeptService::list();

        $data = [
            'list' => $res, 
            'page' => $res->render(),
            'statusList' => BaseStatusEnum::getAll(),
            'sexList' => SexEnum::getAll(),
            'roleList' => SysRoleService::list(),
            'deptList' => flattenMenuArray($deptList),
            'postList' =>SysPostService::list(),
            'params' => $this->request->get()
        ];

        return View::fetch('', $data);
    }

    public function add(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'dept_id' => 'require',
            'username' => 'require',
            'password'  => 'require|regex:/^[a-zA-Z0-9_]{8,20}$/',
            'realname' => 'require',
            'phone' => 'require',
        ];
        //提示信息
        $message = [
            'dept_id.require' => '请选择部门！!',
            'username.require' => '用户名不能为空!',
            'password.require' => '密码不能为空!',
            'password.regex' => '密码长度8~20位，包含字母数字下划线!',
            'realname.require' => '真实姓名不能为空!',
            'phone.require' => '手机号不能为空!',
        ];
        $this->validate($data, $validate, $message);
        return success(SysUserService::save($data));
    }

     //更新
     public function edit(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'id' => 'require',
            'username' => 'require',
        ];
        //提示信息
        $message = [
            'id.require' => 'ID不能为空!',
            'username.require' => '账号不能为空!',
        ];

        if(!empty($data['password'])){
            //如果密码不为空时，说明是修改密码，因此要进行密码校验
            $validate['password'] = 'require|regex:/^[a-zA-Z0-9_]{8,20}$/';
            $message['password.require'] = '密码不能为空!';
            $message['password.regex'] = '密码长度8~20位，包含字母数字下划线!';
        }else{
            //销毁变量
            unset($data['password']);
        }

        $this->validate($data, $validate, $message);

        return success(SysUserService::update($data));
    }

    // 详情
    public function info() {
        $id = $this->request->get("id");
        if (empty($id)) {
            return success();
        }
        return success(SysUserService::info($id));
    }

    //删除
    public function delete(){
        $ids = $this->request->get("ids");
        if(empty($ids)){
            return failure(ErrorEnum::SELECT_DELETE_DATA_CODE, ErrorEnum::SELECT_DELETE_DATA_DESC);
        }

        SysUserService::destroy($ids);
        return success();
    }

    // 修改密码
    public function changePassword() {
        $data = $this->request->post();
        //验证规则
        $validate = [
            'password'  => 'require|regex:/^[a-zA-Z0-9_]{8,20}$/',
        ];
        //提示信息
        $message = [
            'password.require' => '密码不能为空!',
            'password.regex' => '密码长度8~20位，包含字母数字下划线!',
        ];
        $this->validate($data, $validate, $message);
        if ($data['password'] != $data['repassword']) {
            return failure(ErrorEnum::ENTERED_PASSWORDS_DIFFER_CODE, ErrorEnum::ENTERED_PASSWORDS_DIFFER_DESC);
        }

        $data['id'] = session('user_id');
        SysUserService::changePasswrod($data);

        return success();
    }

    // 退出登录
    public function logout(){
        session("userInfo", null);
        session("user_id", null);
        return success();
    }

}
